package com.eplayer.mqdx.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.eplayer.mqdx.R;
import com.eplayer.mqdx.college.fragment.BookShelfFragment;
import com.eplayer.mqdx.college.fragment.ExpertFragment;
import com.eplayer.mqdx.college.fragment.RecommendCollegeFragment;
import com.eplayer.mqdx.college.fragment.SchoolFragment;
import com.eplayer.mqdx.college.fragment.VideoFragment;
import com.eplayer.mqdx.view.LazyViewPager;
import com.eplayer.mqdx.view.LazyViewPager.OnPageChangeListener;
import com.nineoldandroids.view.ViewHelper;

public class CollegeFragment extends Fragment {
	private RadioGroup rg_college_line;
	private RadioButton rb_college_recommend, rb_college_school,
			rb_college_video, rb_college_expert, rb_college_bookShelf;
	private LazyViewPager college_viewpager;
	private View college_indicate_line;
	private View view;
	private List<Fragment> lists = new ArrayList<Fragment>();
	private int lineWidth;
	private int listsSize;
	private float endX;
	private RecommendCollegeFragment recommendCollegeFragment;
	private SchoolFragment schoolFragment;
	private VideoFragment videoFragment;
	private ExpertFragment expertFragment;
	private BookShelfFragment bookShelfFragment;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

		view = inflater.inflate(R.layout.fragment_college, null);
		if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {

			// 透明状态栏

			getActivity().getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

			// 透明导航栏

			getActivity().getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

		}
		initView();
		initDate();
		return view;
	}

	private void initView() {
        rg_college_line = (RadioGroup) view.findViewById(R.id.rg_college_line);
        rb_college_recommend = (RadioButton) view.findViewById(R.id.rb_college_recommend);
        rb_college_school = (RadioButton) view.findViewById(R.id.rb_college_school);
        rb_college_video = (RadioButton) view.findViewById(R.id.rb_college_video);
        rb_college_expert = (RadioButton) view.findViewById(R.id.rb_college_expert);
        rb_college_bookShelf = (RadioButton) view.findViewById(R.id.rb_college_bookShelf);
        college_viewpager = (LazyViewPager) view.findViewById(R.id.college_viewpager);
        college_indicate_line = view.findViewById(R.id.college_indicate_line);
        
        
        recommendCollegeFragment = new RecommendCollegeFragment();
        schoolFragment = new SchoolFragment();
        videoFragment = new VideoFragment();
        expertFragment = new ExpertFragment();
        bookShelfFragment = new BookShelfFragment();
        lists.clear();
        lists.add(recommendCollegeFragment);
        lists.add(schoolFragment);
        lists.add(videoFragment);
        lists.add(expertFragment);
        lists.add(bookShelfFragment);
        // 指示线初始化 (屏幕的一半)
        rb_college_recommend.setChecked(true);
        int screenWidth = getResources().getDisplayMetrics().widthPixels;// 屏幕宽度
		lineWidth = screenWidth / lists.size();
		college_indicate_line.getLayoutParams().width = lineWidth;
	}

	private void initDate() {
        //添加适配器
		CollegeAdapter adapter = new CollegeAdapter(getChildFragmentManager());
		college_viewpager.setAdapter(adapter);
		// 添加滑动监听事件
		college_viewpager.setOnPageChangeListener(new OnCollegePageChangeListener());
		// 添加点击事件
		rg_college_line.setOnCheckedChangeListener(new OnCollegeCheckedChangeListener());
	}
	
	private final class OnCollegePageChangeListener implements
			OnPageChangeListener {
		@Override
		public void onPageSelected(int position) {
			/**
			 * 1. 标签颜色改变 2. 指示线跟着手指一起变化?
			 */
			// 1. 标签颜色改变
			int check = getResources().getColor(R.color.home_title_true);
			int moren = getResources().getColor(R.color.home_title_false);
			rb_college_recommend.setTextColor(position == 0 ? check : moren);
			rb_college_school.setTextColor(position == 1 ? check : moren);
			rb_college_video.setTextColor(position == 2 ? check : moren);
			rb_college_expert.setTextColor(position == 3 ? check : moren);
			rb_college_bookShelf.setTextColor(position == 4 ? check : moren);
		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
			float disX = positionOffset * lineWidth;
			float startX = position * lineWidth;
			endX = startX + disX;
			ViewHelper.setTranslationX(college_indicate_line, endX);// 直接改变属性(位移坐标)
		}

		@Override
		public void onPageScrollStateChanged(int state) {
			
		}
	}
	private final class OnCollegeCheckedChangeListener implements
			OnCheckedChangeListener {
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch (checkedId) {
			case R.id.rb_college_recommend:
				college_viewpager.setCurrentItem(0);
				break;
			case R.id.rb_college_school:
				college_viewpager.setCurrentItem(1);
				break;
			case R.id.rb_college_video:
				college_viewpager.setCurrentItem(2);
				break;
			case R.id.rb_college_expert:
				college_viewpager.setCurrentItem(3);
				break;
			case R.id.rb_college_bookShelf:
				college_viewpager.setCurrentItem(4);
				break;

			default:
				break;
			}
		}
	}
	class CollegeAdapter extends FragmentPagerAdapter{

		public CollegeAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			return lists.get(position);
		}

		@Override
		public int getCount() {
			return lists.size();
		}
		
	}
	@Override
    public void onResume() {
    	super.onResume();
    	ViewHelper.setTranslationX(college_indicate_line, endX);
    }
}
